<html>
<head><meta charset="utf-8"><title>Adding lazy static vars to std · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html">Adding lazy static vars to std</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206298194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206298194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206298194">(Aug 07 2020 at 19:13)</a>:</h4>
<p>I don’t know if this has been proposed before, but has there been consideration for adding <code>once_cell</code> or an equivalent to std? Lazily initialised static variables are one of the most common patterns in Rust. <code>lazy_static</code>, <code>lazycell</code> and <code>once_cell</code> combined have about 37 million downloads on <a href="http://crates.io">crates.io</a>; which makes them the more popular than <code>rand</code>. Looking at the code for all of these implementations  are all relatively straightforward and small, so it seems like it wouldn't have a substantial impact on the std library while providing huge amount of utility.</p>
<p>Cc <span class="user-mention" data-user-id="133169">@matklad</span></p>



<a name="206298580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206298580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206298580">(Aug 07 2020 at 19:16)</a>:</h4>
<p>You mean like this? <a href="https://doc.rust-lang.org/nightly/core/lazy/struct.OnceCell.html">https://doc.rust-lang.org/nightly/core/lazy/struct.OnceCell.html</a> :D</p>



<a name="206310773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206310773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206310773">(Aug 07 2020 at 21:18)</a>:</h4>
<p>Oh wow, I love when something I want is already done. <span aria-label="big smile" class="emoji emoji-1f604" role="img" title="big smile">:big_smile:</span></p>



<a name="206338227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206338227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206338227">(Aug 08 2020 at 08:22)</a>:</h4>
<p>that's a non-thread-safe version of this API though, isn't it?</p>



<a name="206338231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206338231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206338231">(Aug 08 2020 at 08:22)</a>:</h4>
<p>I think correctness crucially relies on this being sequential, but the comments do not say so</p>



<a name="206344929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206344929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206344929">(Aug 08 2020 at 11:56)</a>:</h4>
<p>There's thread safe versions in std</p>



<a name="206344957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206344957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206344957">(Aug 08 2020 at 11:56)</a>:</h4>
<p>But the core one is !Sync</p>



<a name="206391641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206391641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206391641">(Aug 09 2020 at 11:49)</a>:</h4>
<p>ah that makes sense, thanks</p>



<a name="206391685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Adding%20lazy%20static%20vars%20to%20std/near/206391685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Adding.20lazy.20static.20vars.20to.20std.html#206391685">(Aug 09 2020 at 11:50)</a>:</h4>
<p>but AFAIK lazy_static needs something no_std that is <code>Sync</code>? this is what leads to <a href="https://github.com/rust-lang-nursery/lazy-static.rs/issues/150">https://github.com/rust-lang-nursery/lazy-static.rs/issues/150</a> which I still think is highly problematic</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>